// 定时器数组
var timeidArr = []
// dom对象
var svg = document.querySelector("svg");
var button = document.querySelector("#button");
var orderDom = document.querySelector("#order");
var selectDom = document.querySelector("#select");
var deleyDom = document.querySelector("#deley");
var numDom = document.querySelector("#num");
var getArrDom = document.querySelector("#getArr");
var stopDom = document.querySelector("#stop");
var compareDom = document.querySelector("#compare");
var viewSource = document.querySelector("#viewSource")
var source = document.querySelector(".source")
var sourceCode = document.querySelector("#sourceCode")
var h1 = document.querySelector("h1");
var h2 = document.querySelector("h2")
var h3 = document.querySelector('h3')
var h4 = document.querySelector('h4')
var table = document.querySelector("table");
// 算法源码dom对象
var shellSortSource = document.querySelector(".shellSortSource");
var bubbleSortSource = document.querySelector(".bubbleSortSource")
var insertSortSource = document.querySelector(".insertSortSource")
var selectSortSource = document.querySelector(".selectSortSource")
var quickSortSource = document.querySelector('.quickSortSource');
// 源码显示初始化状态
isHidden = true
// 生成数组需要的属性声明
var arr = [2, 8, 7, 4, 1, 5];
let margin_level = 20; // 水平两端距离
let margin_vertical = 20; //垂直两端距离
let width = svg.getAttribute("width");
let height = svg.getAttribute("height");
// 动画需要次数
var show_count = 0;
// 动画已执行次数
var deley_count = 0;
// 动画执行间隔
var deley_space = 1000;
// 随机生成数组
function randomArr(num, max, min) {
    if (typeof max !== "number" || max < 100) {
        max = 100;
    }
    if (typeof min !== "number") {
        min = 1;
    }
    let arr = [];
    for (let i = 0; i < num; i++) {
        arr[i] = Math.floor(Math.random() * (max - min + 1) + min);
    }
    return arr;
}
// 获取数组最大的元素值
function getMax(arr) {
    let max = arr[0];
    for (let i = 1; i < arr.length; i++) {
        if (max < arr[i]) {
            max = arr[i];
        }
    }
    return max;
}
// 获取数组每一个元素在页面所占高度
function getHeight(i, max) {
    return (height - 2 * margin_vertical) * (i / max) * 0.9;
}
// 比较效率专用获取高度方法
function getEfficiencyHeight(i, max) {
    if (max == 0) {
        return 1;
    } else if (max == null) {
        return i;
    }
    return (i / max) * (height - 5 * margin_vertical)
}

// svg渲染数组
function showArr(arr, m, n, sort, t) { // t表示轮数 为选择排序设计
    h3.innerText = ''
    h2.innerText = ''
    h4.innerText = ''
    var max = getMax(arr);
    let textStr = "";
    let rectStr = "";
    let lineStr = "";
    table.innerHTML = '<tr><th>变量名</th><th>索引</th><th>值</th><th>注释</th></tr>'
    let tableStr = ''
    svg.innerHTML = "";
    let rectWidth = (width - 2 * margin_level) / arr.length * 0.6;
    let space_width = rectWidth * 2 / 3; // 柱形间隔距离
    let sameArr = []; // 希尔排序 存放同一组颜色
    if (sort == shellSort) {
        let gap = n - m;
        // 往后找同组元素
        for (let i = m; i < arr.length; i += gap) {
            sameArr.push(i);
        }
        // 往前找同组元素
        for (let i = m; i >= 0; i -= gap) {
            sameArr.push(i);
        }
    }
    for (let i = 0; i < arr.length; i++) {
        let color = '#5cacee'
        // if (i == m) {
        //     color = "black";
        // }
        // if (i == n) {
        //     color = "red";
        // }
        let rect_height = getHeight(arr[i], max);
        let rx = margin_vertical + i * (space_width + rectWidth);
        let ry = (height - margin_vertical * 2 - rect_height)
        let font = (rectWidth / 2 > 20 ? 20 : rectWidth / 2);

        // 画图前判断一下是否为特定算法

        // 希尔排序
        if (sort == shellSort) {
            // 把同组元素标记为粉色
            for (let j = 0; j < sameArr.length; j++) {
                if (i == sameArr[j]) {
                    color = 'pink'
                    break;
                }
            }
            let gap = n - m;

            // 文字
            h2.innerText = '当前gap为[' + gap + ']'
            h3.innerHTML = '需插入元素指针[' + (n) + '] vs  比较元素指针[' + m + ']'

            // 表格
            tableStr += '<tr><th>变量名</th><th>索引</th><th>值</th><th>注释</th></tr>'
            tableStr += '<tr><td>gap</td><td>无索引</td><td>' + gap + '</td><td>步长</td></tr>'
            tableStr += '<tr><td>j+gap</td><td>' + n + '</td><td>' + arr[n] + '</td><td>需插入元素指针</td></tr>'
            tableStr += '<tr><td>j</td><td>' + m + '</td><td>' + arr[m] + '</td><td>比较元素指针</td></tr>'
            table.innerHTML = tableStr
            tableStr = ''
            // 把需插入元素指针用文字标记
            if (i == n) {
                textStr += '<text x="' + (rx + rectWidth / 2) + '"y="' + (ry - 40) + '"style="text-anchor:middle;font-size:' + (font - 6) + 'px"fill="#171717">' + '需插入元素指针' + '</text>'
            }
            if (i == m) {
                textStr += '<text x="' + (rx + rectWidth / 2) + '"y="' + (ry - 40) + '"style="text-anchor:middle;font-size:' + (font - 6) + 'px"fill="#171717">' + '比较元素指针' + '</text>'
            }
        }

        // 冒泡排序
        else if (sort == bubbleSort) {
            // 文字
            h2.innerText = '当前为第' + m + '轮排序'
            h3.innerText = '当前元素指针[' + (n) + '] vs  比较元素指针[' + (n + 1) + ']'

            // 表格
            tableStr += '<tr><th>变量名</th><th>索引</th><th>值</th><th>注释</th></tr>'
            tableStr += '<tr><td>i</td><td>无索引</td><td>' + m + '<td>第几轮排序</td></tr>'
            tableStr += '<tr><td>j</td><td>' + n + '</td><td>' + arr[n] + '</td><td>当前元素指针</td></tr>'
            tableStr += '<tr><td>j+1</td><td>' + (n + 1) + '</td><td>' + arr[n + 1] + '</td><td>比较元素指针</td></tr>'
            table.innerHTML = tableStr
            tableStr = ''
            if (i == n) {
                textStr += '<text x="' + (rx + rectWidth / 2) + '"y="' + (ry - 40) + '"style="text-anchor:middle;font-size:' + (font - 6) + 'px"fill="#171717">' + '当前元素指针' + '</text>'
                color = 'pink'
            }
            if (i == n + 1) {
                color = 'grey'
                textStr += '<text x="' + (rx + rectWidth / 2) + '"y="' + (ry - 40) + '"style="text-anchor:middle;font-size:' + (font - 6) + 'px"fill="#171717">' + '比较元素' + '</text>'
            }
        }

        // 插入排序
        else if (sort == insertSort) {
            h2.innerText = '当前需插入数字为' + m
            h3.innerHTML = '需插入元素指针[' + (n + 1) + '] vs  已排序元素指针[' + n + ']'

            // 表格
            tableStr += '<tr><th>变量名</th><th>索引</th><th>值</th><th>注释</th></tr>'
            tableStr += '<tr><td>j+1</td><td>' + (n + 1) + '</td><td>' + arr[n + 1] + '<td>当前需插入元素指针</td></tr>'
            tableStr += '<tr><td>j</td><td>' + n + '</td><td>' + arr[n] + '</td><td>比较元素指针</td></tr>'
            tableStr += '<tr><td>temp</td><td>无索引</td><td>' + m + '</td><td>需插入元素</td></tr>'
            table.innerHTML = tableStr
            tableStr = ''
            if (i == (n + 1)) {
                textStr += '<text x="' + (rx + rectWidth / 2) + '"y="' + (ry - 40) + '"style="text-anchor:middle;font-size:' + (font - 6) + 'px;"fill="#171717">' + '需插入元素指针' + '</text>'
                color = 'pink'
            }
            if (i == n) {
                textStr += '<text x="' + (rx + rectWidth / 2) + '"y="' + (ry - 40) + '"style="text-anchor:middle;font-size:' + (font - 6) + 'px;"fill="#171717">' + '比较元素指针' + '</text>'
                color = 'grey'
            }
        }

        // 选择排序
        else if (sort == selectSort) {
            h2.innerText = '当前为第[' + (t + 1) + ']轮比较'
            h3.innerText = '当前index元素为' + arr[m]
            h4.innerText = 'index表示最大值或最小值(取决于升序降序)'

            // 表格
            tableStr += '<tr><th>变量名</th><th>索引</th><th>值</th><th>注释</th></tr>'
            tableStr += '<tr><td>i+1</td><td>无索引</td><td>' + (t + 1) + '<td>第几轮排序</td></tr>'
            tableStr += '<tr><td>i</td><td>' + t + '</td><td>' + arr[t] + '</td><td>当前元素指针</td></tr>'
            tableStr += '<tr><td>index</td><td>' + m + '</td><td>' + arr[m] + '</td><td>最大值或最小值</td></tr>'
            tableStr += '<tr><td>j</td><td>' + n + '</td><td>' + arr[n] + '</td><td>比较元素指针</td></tr>'
            table.innerHTML = tableStr
            tableStr = ''
            if (i == n) {
                color = 'red'
                textStr += '<text x="' + (rx + rectWidth / 2) + '"y="' + (ry - 40) + '"style="text-anchor:middle;font-size:' + (font - 6) + 'px;"fill="#171717">' + '比较元素指针' + '</text>'
            }
            if (i == m) {
                color = 'grey'
                textStr += '<text x="' + (rx + rectWidth / 2) + '"y="' + (ry - 70) + '"style="text-anchor:middle;font-size:' + (font - 6) + 'px;"fill="#171717">' + 'index指针' + '</text>'
            }
            if (i == t) {
                textStr += '<text x="' + (rx + rectWidth / 2) + '"y="' + (ry - 40) + '"style="text-anchor:middle;font-size:' + (font - 6) + 'px;"fill="#171717">' + '当前元素指针' + '</text>'
                color = 'pink'
            }

        }

        //快速排序
        else if (sort == quickSort) { // 5 右指针往左 6 左指针往右 7 指针重合
            // 表格
            tableStr += '<tr><th>变量名</th><th>索引</th><th>值</th><th>注释</th></tr>'
            tableStr += '<tr><td>i</td><td>' + m + '</td><td>' + arr[m] + '<td>左指针</td></tr>'
            tableStr += '<tr><td>j</td><td>' + n + '</td><td>' + arr[n] + '</td><td>右元素指针</td></tr>'
            table.innerHTML = tableStr
            tableStr = ''
            // 5 
            if (i == m) {
                color = "pink";
                textStr += '<text x="' + (rx + rectWidth / 2) + '"y="' + (ry - 40) + '"style="text-anchor:middle;font-size:' + (font - 8) + 'px;"fill="#171717">' + '左指针' + '</text>'
            }
            if (i == n) {
                textStr += '<text x="' + (rx + rectWidth / 2) + '"y="' + (ry - 30) + '"style="text-anchor:middle;font-size:' + (font - 8) + 'px;"fill="#171717">' + '右指针' + '</text>'
                color = "grey";
            }
            if (t == 5) {
                h2.innerText = '右指针往左移动'
                h3.innerText = '寻找比左指针[' + m + ']小的元素'
            }
            else if (t == 6) {
                h2.innerText = '左指针向右移动'
                h3.innerText = '寻找比右指针[' + n + ']小的元素'
            }
            else if (t == 7) {
                h2.innerText = '左右指针元素进行交换'
                h3.innerText = '将两边元素进行对调'
            }
            else if (t == 8) {
                h2.innerText = '左右指针元素重合'
                h3.innerText = '当前轮已排序完毕'
            }

        }



        rectStr += '<rect x="' + rx + '"y="' + ry + '"fill="' + color + '"width="' + rectWidth + '"height="' + rect_height + '"></rect>'
        textStr += '<text x="' + (rx + rectWidth / 2) + '"y="' + (ry - 5) + '"style="text-anchor:middle;font-size:' + font + 'pt"fill="#171717">' + arr[i] + '</text>'
            + '<text x="' + (rx + rectWidth / 2) + '"y="' + (height - 5) + '"style="text-anchor:middle;font-size:' + font + 'pt">' + (i + 1) + '</text>'
    }
    // 画坐标轴线
    lineStr += '<line x1="0" x2="0" y1="0" y2="' + (height - 2 * margin_vertical + 4) + '"style="stroke:#666;stroke-width:3"/>' +
        '<line x1="0" x2="' + (width - 5) + '"y1="' + (height - 2 * margin_vertical + 2) + '"y2="' + (height - 2 * margin_vertical + 2) + '"style="stroke:#666;stroke-width:1"/>'
    svg.innerHTML = textStr + lineStr + rectStr
}
// svg渲染算法时间效率
// svg渲染数组
function showEfficiency(timeArr, NameArr, arr) {
    // 当数组长度超过2000时 不计算最大值 直返回数组高度 优化效率
    var max = null;
    if (arr.length <= 2000) {
        max = getMax(timeArr);
    }
    let textStr = "";
    let rectStr = "";
    let lineStr = "";
    svg.innerHTML = "";
    let rectWidth = (width - 2 * margin_level) / timeArr.length * 0.6;
    let space_width = rectWidth * 2 / 3; // 柱形间隔距离
    for (let i = 0; i < timeArr.length; i++) {
        let color = '#5cacee'
        let rect_height = getEfficiencyHeight(timeArr[i], max)
        if (rect_height == 0) {
            rect_height = 1;
        }
        let rx = margin_vertical + i * (space_width + rectWidth);
        let ry = (height - margin_vertical * 2 - rect_height)
        let font = (rectWidth / 2 > 20 ? 20 : rectWidth / 2);
        rectStr += '<rect x="' + rx + '"y="' + ry + '"fill="' + color + '"width="' + rectWidth + '"height="' + rect_height + '"></rect>'
        textStr += '<text x="' + (rx + rectWidth / 2) + '"y="' + (ry - 5) + '"style="text-anchor:middle;font-size:' + font + 'pt"fill="#171717">' + timeArr[i] + '</text>'
            + '<text x="' + (rx + rectWidth / 2) + '"y="' + (height - 5) + '"style="text-anchor:middle;font-size:' + font + 'pt">' + NameArr[i] + '</text>'
    }
    // 画坐标轴线
    lineStr += '<line x1="0" x2="0" y1="0" y2="' + (height - 2 * margin_vertical + 4) + '"style="stroke:#666;stroke-width:3"/>' +
        '<line x1="0" x2="' + (width - 5) + '"y1="' + (height - 2 * margin_vertical + 2) + '"y2="' + (height - 2 * margin_vertical + 2) + '"style="stroke:#666;stroke-width:1"/>'
    svg.innerHTML = textStr + lineStr + rectStr
}
// 演示动画
function showSort(arr, m, n, sort, i) { // i为当前轮数 为选择排序设置
    let array = [];
    for (let i = 0; i < arr.length; i++) {
        array[i] = arr[i];
    }
    let callback = function () {
        showArr(array, m, n, sort, i);
        deley_count++;
        if (deley_count >= show_count) {
            showArr(arr);
            deley_count = 0;
            show_count = 0;
            console.log("数组排序完成")
            h2.innerText= '排序已完成'
        }
    }
    let a = setTimeout(callback, deley_space * show_count++)
    timeidArr.push(a)
}

// 希尔排序
function shellSort(arr, order, showSort) {
    let gap = Math.floor(arr.length / 2);
    while (gap > 0) {
        for (let i = gap; i < arr.length; i++) {
            for (let j = i - gap; j >= 0; j -= gap) {
                if (showSort != null) {
                    showSort(arr, j, j + gap, shellSort)
                }
                if (arr[j + gap] * order < arr[j] * order) {
                    let temp = arr[j + gap];
                    arr[j + gap] = arr[j];
                    arr[j] = temp;
                    if (showSort != null) {
                        showSort(arr, j, j + gap, shellSort)
                    }
                }
            }
        }
        gap = Math.floor(gap / 2);
    }
}

// 希尔排序 比较效率版
function shellSort2(arr, order, showSort) {
    let gap = Math.floor(arr.length / 2);
    while (gap > 0) {
        for (let i = gap; i < arr.length; i++) {
            let temp = arr[i];
            for (var j = i - gap; j >= 0 && temp * order < arr[j] * order; j -= gap) {
                if (showSort != null) {
                    showSort(arr, j, j + gap)
                }
                arr[j + gap] = arr[j];
                if (showSort != null) {
                    showSort(arr, j, j + gap)
                }
            }
            arr[j + gap] = temp;
            if (showSort != null) {
                showSort(arr, j, j + gap)
            }
        }
        gap = Math.floor(gap / 2);
    }
}

// 冒泡排序
function bubbleSort(arr, order, showSort) {
    for (let i = 1; i < arr.length; i++) {
        let flag = 0;
        for (let j = 0; j < arr.length - i; j++) {
            if (showSort != null) {
                showSort(arr, i, j, bubbleSort); // i 当前轮 j 指针元素
            }
            if (arr[j] * order > arr[j + 1] * order) {
                let temp = arr[j + 1];
                arr[j + 1] = arr[j];
                arr[j] = temp;
                flag = 1;
            }
            if (showSort != null) {
                showSort(arr, i, j, bubbleSort);
            }
        }
        if (flag == 0) {
            console.log('flag', flag)
            return;
        }

    }
}

// 插入排序
function insertSort(arr, order, showSort) {
    for (let i = 1; i < arr.length; i++) {
        let temp = arr[i];
        for (var j = i - 1; j >= 0; j--) {
            if (showSort != null) {
                showSort(arr, temp, j, insertSort);
            }
            if (temp * order < arr[j] * order) {
                arr[j + 1] = arr[j]
                if (showSort != null) {
                    showSort(arr, temp, j, insertSort);
                }
            } else {
                break;
            }
        }
        arr[j + 1] = temp;
        if (showSort != null) {
         
        }

    }
}

//  选择排序
function selectSort(arr, order, showSort) {
    for (let i = 0; i < arr.length; i++) {
        let index = i;
        for (let j = i + 1; j < arr.length; j++) {
            if (showSort != null) {
                // i为当前轮数
                showSort(arr, index, j, selectSort, i);
            }
            if (arr[index] * order > arr[j] * order) {
                index = j;
                if (showSort != null) {
                    showSort(arr, index, j, selectSort, i);
                }
            }
        }
        let temp = arr[i];
        arr[i] = arr[index];
        arr[index] = temp;

    }
}

//  快速排序
var quickSort = function (arr, order, showSort) {
    // 快速排序
    var quick = function (arr, first, end) {
        if (first < end) {
            let i = first, j = end, temp = 0;
            // 一个循环完成一趟扫描
            while (i < j) {
                while (i < j && order * arr[i] < order * arr[j]) {
                    if (showSort != null) {
                        showSort(arr, i, j, quickSort, 5);
                    }
                    j--;
                }
                if (i < j) {
                    if (showSort != null) {
                        showSort(arr, i, j, quickSort, 5);
                    }
                    temp = arr[i];
                    arr[i] = arr[j];
                    arr[j] = temp;
                    if (showSort != null) {
                        // 指针元素交换
                        showSort(arr, i, j, quickSort, 7);
                    }
                    i++;
                }
                while (i < j && order * arr[i] < order * arr[j]) {
                    if (showSort != null) {
                        showSort(arr, i, j, quickSort,7);
                    }
                    i++;
                }
                if (i < j) {
                    if (showSort != null) {
                        showSort(arr, i, j, quickSort, 6);
                    }
                    temp = arr[i];
                    arr[i] = arr[j];
                    arr[j] = temp;
                    if (showSort != null) {
                        showSort(arr, i, j, quickSort, 7);
                    }
                    j--;
                }
            }
            if (showSort != null) {
                showSort(arr, i, i, quickSort, 8);
            }
            quick(arr, first, i - 1);
            quick(arr, i + 1, end);
        }
        return arr;
    }

    return quick(arr, 0, arr.length - 1);
}


// 求代码执行时间 参数 arr数组 sort排序方法 order倒叙正序
function getRunningTime(arr, sort, order) {
    // new Date().getTime() 误差比较大
    let arr1 = [];
    for (let j = 0; j < arr.length; j++) {
        arr1[j] = arr[j];
    }
    let startTime = performance.now();
    sort(arr1, order)
    let endTime = performance.now();
    return (endTime - startTime).toFixed(2);
}

// 清空所有动画
function clearAnimation() {
    // 设置一个定时器 获取返回值
    let end = setTimeout(function () { }, 100000);
    for (let i = 0; i <= end; i++) {
        clearTimeout(i);
    }
    // console.log('ss')
    // for(let i=0;i<timeidArr.length;i++){
    //     console.log("time",timeidArr[i])
    //     clearTimeout(timeidArr[i]);
    // }
    // timeidArr = [];
    deley_count = 0;
    show_count = 0;
}

// 获取一个新数组
function getNewArr(arr) {
    let array = [];
    for (let i = 0; i < arr.length; i++) {
        array[i] = arr[i];
    }
    return array;
}



// 用循环写一个暂停的按钮

/* 暂时不会 以后再写*/

// 初始化
var init = function () {
    console.log("待排序的数组", arr);
    showArr(arr)
    h1.innerText = '算法可视化'
    h2.innerText = ''
    let tStr = '<tr><th>变量名</th><th>索引</th><th>值</th><th>注释</th></tr>'
    tStr += '<tr><td>gap</td><td>无索引</td><td>1</td><td>步长</td></tr>'
    tStr += '<tr><td>j+gap</td><td>3</td><td>4</td><td>需插入元素指针</td></tr>'
    tStr += '<tr><td>j</td><td>0</td><td>2</td><td>比较元素指针</td></tr>'
    table.innerHTML = tStr;
    getArrDom.addEventListener("click", function () {
        h2.innerText = ''
        h3.innerText = ''
        h4.innerText = ''
        let num = Number(numDom.value.trim());
        if (num == "" || num < 0) {
            alert("您输入的数字不正确,请重新输入!")
        }
        if (num != NaN && num > 0) {

            arr = randomArr(num, num, 1)
            console.log("随机生成的数组为", arr)
            clearAnimation()
            showArr(arr)
            h2.innerText = '随机生成数组完成'
            h3.innerText = '建议用户在数组长度为700以内排序,防止操作过多dom导致浏览器卡死'
        }
    });

    // 隐藏源码
    function sourceHidden() {
        viewSource.value = '打开显示当前源码';
        shellSortSource.style.display = 'none'
        bubbleSortSource.style.display = 'none'
        insertSortSource.style.display = 'none'
        selectSortSource.style.display = 'none'
        quickSortSource.style.display = 'none'
    }

    // 显示源码
    function sourceVisible(index) {
        viewSource.value = '关闭显示当前源码';
        switch (index) {
            case 0:
                shellSortSource.style.display = 'block'
                break;
            case 1:
                bubbleSortSource.style.display = 'block'
                break;
            case 2:
                insertSortSource.style.display = 'block'
                break;
            case 3:
                selectSortSource.style.display = 'block'
                break;
            case 4:
                quickSortSource.style.display = 'block'
                break;

        }
    }
    button.addEventListener("click", function () {
        h1.innerText = '进行排序可视化'
        deley_space = isNaN(Number(deleyDom.value.trim())) ? 1000 : Number(deleyDom.value.trim())
        var order = orderDom.selectedIndex === 0 ? 1 : -1
        var index = selectDom.selectedIndex;
        clearAnimation();
        if (isHidden == false) {
            sourceHidden();
            sourceVisible(index);
        }
        let a1
        switch (index) {
            case 0:
                h1.innerText += '(希尔排序)'
                a1 = getNewArr(arr);
                shellSort(a1, order, showSort);
                break;
            case 1:
                h1.innerText += '(冒泡排序)'
                a1 = getNewArr(arr);
                bubbleSort(a1, order, showSort);
                break;
            case 2:
                h1.innerText += '(插入排序)'
                a1 = getNewArr(arr);
                insertSort(a1, order, showSort);
                break;
            case 3:
                h1.innerText += '(选择排序)'
                a1 = getNewArr(arr);
                selectSort(a1, order, showSort);
                break;
            case 4:
                h1.innerText += '(快速排序)'
                a1 = getNewArr(arr);
                quickSort(a1, order, showSort);
                break;
        }
    })
    compareDom.addEventListener("click", function () {
        table.innerHTML = ''
        h2.innerText = ''
        h3.innerText = ''
        h4.innerText = ''
        h1.innerText = '算法效率比较 (高度表示为算法所消耗的时间)(单位/微秒)'
        var order = orderDom.selectedIndex === 0 ? 1 : -1
        clearAnimation();
        console.log("开始进行比较效率", arr)
        h2.innerText = '算法开始比较'
        let shellSortTime = getRunningTime(arr, shellSort2, order);
        let selectSortTime = getRunningTime(arr, selectSort, order)
        let bubbleSortTime = getRunningTime(arr, bubbleSort, order);
        let insertSortTime = getRunningTime(arr, insertSort, order);
        let quickSortTime = getRunningTime(arr, quickSort, order)
        let timeArr = [shellSortTime, selectSortTime, bubbleSortTime, insertSortTime, quickSortTime];
        source.style.display = 'none'
        sourceHidden();
        let nameArr = []
        nameArr.push('希尔排序');
        nameArr.push("选择排序");
        nameArr.push('冒泡排序');
        nameArr.push('插入排序');
        nameArr.push('快速排序');
        showEfficiency(timeArr, nameArr, arr);
        h2.innerText = '算法比较完成'
        console.log('排序用时数组', timeArr, nameArr)
        console.log("比较效率完成")
    })
    viewSource.addEventListener('click', function () {
        var index = selectDom.selectedIndex;
        if (!isHidden) {
            isHidden = !isHidden
            source.style.display = "none"
            sourceHidden()
        } else {
            isHidden = !isHidden
            source.style.display = 'block'
            sourceVisible(index);

        }
    })
}
init();